<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px" @close="close" :close-on-click-modal="false">
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="功能类型" prop="functionType">
        <el-input v-model.trim="form.functionType" autocomplete="off" />
      </el-form-item>
      <el-form-item label="功能内容" prop="functionContent">
        <el-input v-model.trim="form.functionContent" autocomplete="off" />
      </el-form-item>
      <el-form-item label="开发人" prop="devUserId">
        <el-select v-model="form.devUserId" placeholder="请选择" filterable>
          <el-option v-for="item in userGroup" :key="item.id" :label="item.realname" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="版号" prop="versionNumber">
        <el-input v-model.trim="form.versionNumber" autocomplete="off" />
      </el-form-item>
      <el-form-item label="是否合并" prop="isMerge">
        <el-radio v-model="form.isMerge" :label="1">是</el-radio>
        <el-radio v-model="form.isMerge" :label="2">否</el-radio>
      </el-form-item>
      <el-form-item label="合并SHA1号" prop="mergeSha1">
        <el-input v-model.trim="form.mergeSha1" autocomplete="off" />
      </el-form-item>
      <el-form-item label="合并人" prop="mergeUserId">
        <el-select v-model="form.mergeUserId" placeholder="请选择">
          <el-option v-for="item in userGroup" :key="item.id" :label="item.realname" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { insertFixtureMachines, updateFixtureMachines, getUserGroup } from '@/api/wordOrder'
  export default {
    name: 'FixtureMachines',
    data() {
      return {
        userGroup: [],
        form: {
          id: '',
          functionType: '',
          functionContent: '',
          devUserId: '',
          versionNumber: '',
          isMerge: 1,
          mergeSha1: '',
          mergeUserId: '',
        },
        rules: {
          functionType: [{ required: true, trigger: 'blur', message: '请输入功能类型' }],
          functionContent: [{ required: true, trigger: 'blur', message: '请输入功能内容' }],
          devUserId: [{ required: true, trigger: 'change', message: '请选择开发人' }],
          versionNumber: [{ required: true, trigger: 'blur', message: '请输入版号' }],
          mergeSha1: [{ required: true, trigger: 'blur', message: '请输入合并SHA1号' }],
          mergeUserId: [{ required: true, trigger: 'change', message: '请选择合并人' }],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {
      this.initUserData()
    },
    methods: {
      async initUserData() {
        const { data } = await getUserGroup()
        this.userGroup = data
      },
      showEdit(row) {
        if (!row) {
          this.title = '新建夹具机标准版代码开发合并情况'
        } else {
          this.title = '编辑夹具机标准版代码开发合并情况'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
        this.$emit('fetchDataFixtureMachine')
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.id) {
              console.log(125, this.form)
              await updateFixtureMachines(this.form)
            } else {
              await insertFixtureMachines(this.form)
            }
            this.$baseMessage('提交成功', 'success')
            this.$refs['form'].resetFields()
            this.dialogFormVisible = false
            this.$emit('fetchDataFixtureMachine')
            this.form = this.$options.data().form
          } else {
            return false
          }
        })
      },
    },
  }
</script>
